<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addApprovalLogsForm" ref="addApprovalLogsRef" :rules="addApprovalLogsrules">
                        <el-form-item label="功能类型" :label-width="formLabelWidth"
                                      prop="type" v-if="addApprovalLogsForm.id">
                            <el-input
                                    v-model="addApprovalLogsForm.type"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写功能类型"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批人工号" :label-width="formLabelWidth"
                                      prop="teacherNumber">
                            <el-input
                                    v-model="addApprovalLogsForm.teacherNumber"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批人工号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批人姓名" :label-width="formLabelWidth"
                                      prop="teahcerName">
                            <el-input
                                    v-model="addApprovalLogsForm.teahcerName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批人姓名"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批时间" :label-width="formLabelWidth"
                                      prop="createTime" v-if="addApprovalLogsForm.id">
                            <el-input
                                    v-model="addApprovalLogsForm.createTime"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批意见" :label-width="formLabelWidth"
                                      prop="remark">
                            <el-input
                                    v-model="addApprovalLogsForm.remark"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批意见"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批条目ID" :label-width="formLabelWidth"
                                      prop="modelId" v-if="addApprovalLogsForm.id">
                            <el-input
                                    v-model="addApprovalLogsForm.modelId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批条目ID"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批级别" :label-width="formLabelWidth"
                                      prop="level" v-if="addApprovalLogsForm.id">
                            <el-input
                                    v-model="addApprovalLogsForm.level"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批级别"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddApprovalLogs",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addApprovalLogsForm: {
                    type: "",
                    teacherNumber: "",
                    teahcerName: "",
                    createTime: "",
                    remark: "",
                    modelId: "",
                    level: "",
                },
                // 校验规则
                addApprovalLogsrules: {
                    teacherNumber: vxRule(true, null, "blur"),
                    teahcerName: vxRule(true, null, "blur"),
                    remark: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getApprovalLogsById(id) {
                httpService("/api/approvalLogs/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addApprovalLogsForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addApprovalLogsForm.id = "";
                    this.addApprovalLogsForm.type = "";
                    this.addApprovalLogsForm.teacherNumber = "";
                    this.addApprovalLogsForm.teahcerName = "";
                    this.addApprovalLogsForm.createTime = "";
                    this.addApprovalLogsForm.remark = "";
                    this.addApprovalLogsForm.modelId = "";
                    this.addApprovalLogsForm.level = "";
            },
            save() {
                this.$refs.addApprovalLogsRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("type", this.addApprovalLogsForm.type);
                        params.append("teacherNumber", this.addApprovalLogsForm.teacherNumber);
                        params.append("teahcerName", this.addApprovalLogsForm.teahcerName);
                        params.append("createTime", this.addApprovalLogsForm.createTime);
                        params.append("remark", this.addApprovalLogsForm.remark);
                        params.append("modelId", this.addApprovalLogsForm.modelId);
                        params.append("level", this.addApprovalLogsForm.level);
                        if (this.addApprovalLogsForm.id) {
                            params.append("id", this.addApprovalLogsForm.id);
                            httpService("/api/approvalLogs/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalLogsList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/api/approvalLogs/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalLogsList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>